package com.may.controller;

import com.may.utils.CurlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: docker-demo
 * @description:
 * @author: Bobby.Ma
 * @create: 2020-07-05 19:06
 **/
@RestController
@RequestMapping
public class DemoController {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private HttpServletRequest request;

    @GetMapping("/g")
    public Map<String,Object> get(HttpServletRequest request){
        LOGGER.info("请求地址为：[{}]，请求 方法为 [{}]",request.getLocalAddr(),"get");
        Map<String, Object> map = new HashMap<>();
        map.put("1","用户1");
        map.put("2","用户2");
        map.put("3","用户3");
        map.put("4","用户4");
        map.put("5","用户5");
        System.out.println(request.getServerName());
        return map;
    }

    @RequestMapping("/t")
    public String gg(){
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String element = headerNames.nextElement();
            System.out.println("header:" + element);
            String header = request.getHeader(element);
            System.out.println("value:" + header);
        }

        String header = request.getHeader("x-api-key");
        System.out.println("x-api-key为：" + header);

        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.forEach((k,v) -> {
            System.out.println("key:" + k);
            System.out.println("value:" + v[0]);
        });

        return header;
    }

    @RequestMapping("/cu")
    public String okCurl(){
        CurlUtil curl = CurlUtil.create()
                .get("http://42.194.140.75:8080/t")
                .header("x-api-key", "C#5RzY")
                .header("Authentication", "xxasfafaf.weggqgqg.qg05508")
                .header("ma-ye", "19941220")
                .contentType("application/json");
        System.out.println(curl.cmdLine());
        return curl.execute();
    }
}
